package com.itheima.tlias08_14.controller;

import com.itheima.tlias08_14.annotation.Log;
import com.itheima.tlias08_14.paramobject.InsertStudent;
import com.itheima.tlias08_14.paramobject.SelectStudentPO;
import com.itheima.tlias08_14.paramobject.UpdateStudentPO;
import com.itheima.tlias08_14.service.impl.StudentServiceImp;
import com.itheima.tlias08_14.utils.ResultUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class StudentController {

    @Resource
    StudentServiceImp studentImp;
    @Autowired
    private StudentServiceImp studentServiceImp;

    @GetMapping("/students")
    public ResultUtil selectPageStudent(SelectStudentPO sp){
        return studentImp.selectStudent(sp);
    }

    @DeleteMapping("/students/{ids}")
    @Log
    public ResultUtil hatchDelete(@PathVariable List<Integer> ids){
        if(ids == null || ids.isEmpty()){
            return ResultUtil.failure("当前选择为空");
        }
        return studentImp.hatchDeleteById(ids) >=1 ? ResultUtil.success("删除成功") : ResultUtil.failure();
    }

    @PostMapping("/students")
    @Log
    public ResultUtil insertStudent(@RequestBody InsertStudent student){
        if(student.getName() == null || student.getName().isEmpty()){
            return ResultUtil.failure("姓名不能为空");
        }
        int i = studentImp.insertStudent(student);
        return i == 1 ? ResultUtil.success() : ResultUtil.failure("该学号已经存在");
    }
    @GetMapping("/students/{id}")
    public ResultUtil selectStudentById(@PathVariable Integer id){
        return ResultUtil.success(studentImp.selectStudentById(id));
    }

    @PutMapping("/students")
    @Log
    public ResultUtil updateStudent(@RequestBody UpdateStudentPO usPO){
        return studentImp.updateStudent(usPO) == 1 ? ResultUtil.success() : ResultUtil.failure("学号已经存在");
    }

    @PutMapping("/students/violation/{id}/{score}")
    @Log
    public ResultUtil updateScore(@PathVariable Integer id,@PathVariable Integer score){
        return studentImp.updateScore(id,score) == 1 ? ResultUtil.success() : ResultUtil.failure();
    }

}
